﻿using DroidLord.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SharpAdbClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DroidLord.app.dy
{
    class KSUtils
    {


        public  static void  startLiveRoom(DeviceData deviceData,string livid)
        {
            if(DeviceManager.isDeviceOnline(deviceData.Serial))
            {
                string cmd = "am start -a android.intent.action.VIEW -d ksnebula://live/play/" + livid;
                AdbManager.executeShellNoRead(deviceData, cmd);
            }
        }

        public static string GetKSLiveId(string shareUrl)
        {
            WebRequest wrt = null;
            WebResponse wrp = null;
            try
            {
                //live-stream-id=
                wrt = WebRequest.Create(shareUrl);
                if (null == wrt)
                {
                    return "";
                }
                wrp = wrt.GetResponse();

                string html = string.Empty;

                using (Stream stream = wrp.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                    {
                        html = sr.ReadToEnd();
                    }
                }
                if (html != string.Empty)
                {
                    //JObject jo = (JObject)JsonConvert.DeserializeObject(html);
                    //string sysTime2 = jo["data"]["t"].ToString();
                    //if (wrp != null)
                    //    wrp.Close();
                    //if (wrt != null)
                    //    wrt.Abort();
                    return html;
                }
                else
                {

                }
                //  MessageBox.Show(year + "-" + month + "-" + day + "" + hour + ":" + minite + ":" + second);

            }
            catch (WebException e)
            {
                //MessageBox.Show("WebException:" + e.ToString());
                //System.DateTime currentTime = System.DateTime.Now;
                //return  currentTime;

            }
            catch (Exception e)
            {
                //MessageBox.Show("Exception:" + e.ToString());
                //System.DateTime currentTime = System.DateTime.Now;
                //return currentTime;

            }
            finally
            {
                if (wrp != null)
                    wrp.Close();
                if (wrt != null)
                    wrt.Abort();
            }
            return "";
        }

    }
}
